package cn.bob.pet.service.impl;

import cn.bob.basic.jwt.LoginContext;
import cn.bob.basic.utils.BaiduAuditUtils;
import cn.bob.basic.utils.FreemarkerUtil;
import cn.bob.pet.domain.Pet;
import cn.bob.pet.domain.PetDetail;
import cn.bob.pet.domain.PetOnlineAuditLog;
import cn.bob.pet.mapper.PetDetailMapper;
import cn.bob.pet.mapper.PetMapper;
import cn.bob.pet.mapper.PetOnlineAuditLogMapper;
import cn.bob.pet.service.IPetDetailService;
import cn.bob.pet.service.IPetService;
import cn.bob.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private  PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Value("${pagedir}")
    private String path;

    @Override
    public void add(Pet pet) {
        //添加数据
        super.add(pet);
        //将富文本数据添加到详细表中
        PetDetail petDetail = new PetDetail();
        petDetail.setPetId(pet.getId());
        petDetail.setIntro(pet.getDetail().getIntro());
        petDetail.setAdoptNotice(pet.getDetail().getAdoptNotice());
        petDetailMapper.add(petDetail);
    }

    @Override
    public void delById(Long id) {
        super.delById(id);
        //删除宠物详细信息
        PetDetail detail=petDetailMapper.findPetId(id);
        petDetailMapper.delById(detail.getId());
        //删除审核的信息
        PetOnlineAuditLog petonline= petOnlineAuditLogMapper.findPetId(id);
        petOnlineAuditLogMapper.delById(petonline.getId());
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        //修改宠物详情的数据
        PetDetail petDetail = petDetailMapper.findPetId(pet.getId());
        petDetail.setPetId(pet.getId());
        petDetail.setIntro(pet.getDetail().getIntro());
        petDetail.setAdoptNotice(pet.getDetail().getAdoptNotice());
        petDetailMapper.update(petDetail);

    }

    /**
     * 上架需通过百度ai审核
     * @param ids
     */
    @Override
    public String onsalePet(List<Long> ids) throws IOException {
        //通过id获取到pet对象
        Date date = new Date();
        //创建审核对象表
        PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
        List<Pet> collect = getPets(ids,0L);
        //审核失败的string字符
        StringBuilder sb = new StringBuilder();
        //验证图片信息的方法，违规添加到审核表，不违规添修改pet信息状态以及下架时间，生成页面的静态代码
        checkPicInfo(ids, date, petOnlineAuditLog, collect, sb);
        return sb.toString();
    }
    //验证图片信息的方法，违规添加到审核表，不违规添修改pet信息状态以及下架时间，生成页面的静态代码
    private void checkPicInfo(List<Long> ids, Date date, PetOnlineAuditLog petOnlineAuditLog, List<Pet> collect, StringBuilder sb) throws IOException {
        for (Pet pet : collect) {
            StringBuilder str = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                //判断名字是不通过校验
                str.append("上传的宠物名字违规了，宠物的名字是:").append(pet.getName()).append(";");
                sb.append("【").append(pet.getName()).append("】").append("宠物名字违规");
            }
            if (StringUtils.isNotBlank(pet.getResources())){
                String[] split = pet.getResources().split(",");
                //循环判断图片是否违规
                for (String s : split) {
                    if (!BaiduAuditUtils.ImgCensor(s)){
                        str.append("上传的宠物图片违规了，宠物图片的地址是:").append(pet.getResources()).append(";");
                        sb.append("【").append(pet.getName()).append("】").append("宠物图片违规");
                    }
                }
            }
            //将同一批验证的宠物审核信息进行添加
            if (str.length()>0){
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setNote(str.toString());
                petOnlineAuditLogMapper.add(petOnlineAuditLog);
                continue;
            }
            //不违规创建html页面
            FreemarkerUtil.createFreemark(path, ids);
            //不违规
            pet.setOnsaletime(date);
            pet.setState(1L);
            petMapper.update(pet);
        }
    }

    //下架数据
    @Override
    public void offsalePet(List<Long> ids) {
        //通过id获取到pet对象
        Date date = new Date();
        List<Pet> collect = getPets(ids,1L);
        collect.forEach(pet -> {
            pet.setState(0L);
            pet.setOffsaletime(date);
            petMapper.update(pet);
        });
        FreemarkerUtil.delFreemark(path,ids);
    }
    //查询宠物的详细信息
    @Override
    public Pet bydetail(Long id) {
        return petMapper.bydetail(id);
    }




    //抽取的通过id集合获取的pet对象集合
    private List<Pet> getPets(List<Long> ids,Long state) {
        //传入的数据进行判断如果是已经下架了的id就不管
        List<Long> idlist = ids.stream().filter(id -> petMapper.findById(id).getState() == state).collect(Collectors.toList());
        //通过id获取到pet对象
        Map<Long, Pet> offsales =idlist.stream().collect(Collectors.toMap(id -> id, id -> petMapper.findById(id)));
        //通过id获取到pet对象
        List<Pet> collect = idlist.stream().map(id -> offsales.get(id)).collect(Collectors.toList());
        return collect;
    }
}
